package co.edu.unal.visor.datasource;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JOptionPane;

public class FileReader {
	private static BufferedInputStream bufferedinput;
	//private static FileInputStream fileInput;
	private static InputStream inputStream;
	private static int state=0;
	private static int headerPosition=0;
	String path;

	public FileReader(String path){
		
	}
	public static void openFileInputStream(String path) throws IOException {
		
		if (inputStream != null) {
			closeFileInputStream();
		}
		try {
			inputStream = new FileInputStream(path);
		} catch (FileNotFoundException ex) {
			JOptionPane.showMessageDialog(null, "No se encontro la imagen: "
					+ path, "Error abriendo la imagen", JOptionPane.OK_OPTION);
		}
		// System.out.println("Tamaño de archivo: " + filesize + " bytes");
		bufferedinput = new BufferedInputStream(inputStream);
	}
	
	public static void closeFileInputStream() throws IOException {
		//buffer = null;
		bufferedinput.close();
		inputStream.close();
		headerPosition=0;
	}	

	public static byte[] readBytes(int numBytes) throws IOException{
		byte[] buffer = new byte[numBytes];
		
		if(bufferedinput.read(buffer)==-1){
			state=-1;
		}
		headerPosition+=numBytes;
		return buffer;
	}

	public static int getState() {
		return state;
	}

	public static void setState(int state) {
		FileReader.state = state;
	}

	public static void readBytes(byte[] buffer) throws IOException{
		bufferedinput.read(buffer);
		headerPosition+=buffer.length;
	}
	public static int getHeaderPos() {
		return headerPosition;
	}
	
	public static InputStream getFileStream(){
		return inputStream; 
	}
	
}
